function colorCells() {
	jQuery(".alarm-white").parent().parent().css("background", "white");
	jQuery(".alarm-red").parent().parent().css("background", "red");
	jQuery(".alarm-orange").parent().parent().css("background", "orange");
}


$(function() {

	$('.month-picker')
			.datepicker(
					{
						changeMonth : true,
						changeYear : true,
						dateFormat : 'yy/mm',
						showOn : "button",
						inline : true,
						buttonImage : '../../resources/images/calendar_icon.png',
						buttonImageOnly : true,
						onClose : function(dateText, inst) {
							var month = $(
									"#ui-datepicker-div .ui-datepicker-month :selected")
									.val();
							var year = $(
									"#ui-datepicker-div .ui-datepicker-year :selected")
									.val();
							$(this).datepicker('setDate',
									new Date(year, month, 1));
							$(".ui-datepicker-calendar").hide();
						},
						beforeShow : function(input, inst) {
							if ((datestr = $(this).val()).length > 0) {
								year = datestr.substring(0, 4);
								month = datestr.substring(5, 7) - 1;
								$(this).datepicker('option', 'defaultDate',
										new Date(year, month, 1));
								$(this).datepicker('setDate',
										new Date(year, month, 1));
							}
						}

					});

	$('.date-picker').datepicker({
		dateFormat : 'yy/mm/dd',
		showOn : "button",
		buttonImage : '../../resources/images/calendar_icon.png',
		buttonImageOnly : true
	});

	$('.month-picker').focus(function() {
		$(".ui-datepicker-calendar").hide();
	});
});



function validateDate(form,listDateFields)
{
	var count = 0;
	for(var i = 0;i<listDateFields.length;i++)
	{
		if(checkdate(form.elements["mainForm:"+listDateFields[i]],listDateFields[i]))
		{
			count++;
		}
	}
	
    if(count == listDateFields.length)
	{
    	var yearfield_from  = form.elements["mainForm:Date_From"].value.split("/")[0];
		var monthfield_from = form.elements["mainForm:Date_From"].value.split("/")[1];
		var dayfield_from  = form.elements["mainForm:Date_From"].value.split("/")[2];
		var date_from = new Date(yearfield_from, monthfield_from - 1, dayfield_from);
		
		var yearfield_Until  = form.elements["mainForm:Date_Until"].value.split("/")[0];
		var monthfield_Until = form.elements["mainForm:Date_Until"].value.split("/")[1];
		var dayfield_Until  = form.elements["mainForm:Date_Until"].value.split("/")[2];
		var date_Until = new Date(yearfield_Until, monthfield_Until - 1, dayfield_Until);
		
		if(date_from > date_Until)
		{
			$('#Date_From').show();	
			$('#Date_From').html("Date_from should be less than Date_until");
			
			return false;
		}else
		{
			if(monthfield_Until - monthfield_from > 1)
			{
				$('#Date_From').show();	
				$('#Date_From').html("Month_from and Month_Until should be two sequential months");
				return false;
			}
			else
			{
				$('#Date_From').hide();
				return true;
			}
			
		}
		
    	
	}
    	
    else
	{
    	return false;
	}
}

function validateForm()
{
	var count = 0;
	
	if(validateDatetime())
		count++;
	if(validateText())
		count++;
	
	if(count == 2)
		return true;
	else
		return false;
	
	
}

function validateDatetime()
{
	var form = document.getElementById("mainForm");
	var listDateFields = form.elements["dateIDs"].value.split(",");
	var typeDate = form.elements["typeDate"].value;
	
	if(typeDate == "selectDate")
	{
		return validateDate(form,listDateFields);
	}
	else if(typeDate == "selectMonth")
	{
		return validateMonth(form,listDateFields);
	}
	
	
    	
}

function validateMonth(form,listDateFields)
{
	var count = 0;
	for(var i = 0;i<listDateFields.length;i++)
	{
		if(checkMonth(form.elements["mainForm:"+listDateFields[i]],listDateFields[i]))
		{
			count++;
		}
	}
	
	if(count == listDateFields.length)
	{
    	
		return true;
	}
    	
    else
	{
    	return false;
	}
}



function validateText()
{
	
	var form = document.getElementById("mainForm");
	if(form.elements["textInputIDs"].value.length != 0)
	{
		var listInputFields = form.elements["textInputIDs"].value.split(",");
		var count = 0;
		for(var i = 0;i<listInputFields.length; i++)
		{
			if(checkText(form.elements["mainForm:"+listInputFields[i]],form.elements[listInputFields[i]],listInputFields[i]))
			{
				count++;
			}
		}
		
		if(count == listInputFields.length)
		{
	    	
			return true;
		}
	    	
	    else
		{
	    	return false;
		}
	}
	else
	{
		return true;
	}
	
	
	
}

function checkText(inputValue,InputPattern,id)
{
	var pattern = InputPattern.value;
	var value = inputValue.value;
	var regexp = new RegExp(pattern);
	
	if(value.length == 0)
	{
		$('#'+id).hide();
	    return true;
	}
	else
	{
		if (value.match(regexp)) {
			
			$('#'+id).hide();
		    return true;
		  } else {		  
			$('#'+id).show();
		    return false;
		  }
	}
	
	
}


function checkMonth(input,id)
{
	var validformat = /^\d{4}\/\d{2}$/;
    var returnval = false;
	
	if(input.value.trim() == "" || input.value == "" )
	{
		$('#'+id).show();
		$('#'+id).html("Month is required");
	}
	else
	{
		if (!validformat.test(input.value))
		{
			//alert(id);
			$('#'+id).show();
			$('#'+id).html("Month is invalid");
			returnval = false;
		}
			
		else { // Detailed check for valid date ranges
			var yearfield  = input.value.split("/")[0];
			var monthfield = input.value.split("/")[1];
			var dayobj = new Date(yearfield, monthfield - 1, 1);
			if ((dayobj.getMonth() + 1 != monthfield)
					|| (dayobj.getFullYear() != yearfield))
			{
				$('#'+id).show();
				$('#'+id).html("Date is invalid uuu");
				returnval = false;
			}
				
			else
			{
				$('#'+id).hide();
				returnval = true;
			}
				
		}
	}
	
	return returnval;
}

function checkdate(input,id) {
	var validformat = /^\d{4}\/\d{2}\/\d{2}$/; // Basic check for format
										// validity
	var returnval = false;
	
	if(input.value.trim() == "" || input.value == "" )
	{
		$('#'+id).show();
		$('#'+id).html("Date is required");
	}
	else
	{
		if (!validformat.test(input.value))
		{
			//alert(id);
			$('#'+id).show();
			$('#'+id).html("Date is invalid");
			returnval = false;
		}
			
		else { // Detailed check for valid date ranges
			var yearfield  = input.value.split("/")[0];
			var monthfield = input.value.split("/")[1];
			var dayfield  = input.value.split("/")[2];
			var dayobj = new Date(yearfield, monthfield - 1, dayfield);
			if ((dayobj.getMonth() + 1 != monthfield)
					|| (dayobj.getDate() != dayfield)
					|| (dayobj.getFullYear() != yearfield))
			{
				$('#'+id).show();
				$('#'+id).html("Date is invalid");
				returnval = false;
			}
				
			else
			{
				$('#'+id).hide();
				returnval = true;
			}
				
		}
	}
	
	return returnval;
}
